const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

function getYMDHMS (timestamp) {
  let time = new Date(timestamp)
  let year = time.getFullYear()
  let month = time.getMonth() + 1
  let date = time.getDate()
  let hours = time.getHours()
  let minute = time.getMinutes()
  let second = time.getSeconds()

  if (month < 10) { month = '0' + month }
  if (date < 10) { date = '0' + date }
  if (hours < 10) { hours = '0' + hours }
  if (minute < 10) { minute = '0' + minute }
  if (second < 10) { second = '0' + second }
  return year + '-' + month + '-' + date
  // + ' ' + hours + ':' + minute + ':' + second
}

function treeToArray(data, children = 'children') {
  let tmp = []
  data.forEach((item, index) => {
      tmp.push(item)
      if (item[children] && item[children].length > 0) {
          const res = treeToArray(item[children], children)
          tmp = tmp.concat(res)
      }
  })
  return tmp
}

module.exports = {
  formatTime,
  getYMDHMS,
  treeToArray
}
